let LocalStorageKey = {
    AppVersion: `AppVersion`,
    InnerVideoCDTime: `innerVideoCDTime`,
    InnerSaveLevel: `innerSaveLevel`,
    InnerSafeTime: `innerSafeTime`,
    AudioConfig: `AudioConfig`,
}

/**
 * 数据缓存工具单例
 */
export class LocalStorage {
    readonly LocalStorageKey = LocalStorageKey

    private _data: { [key: string]: unknown }

    //==============================================================================================================================
    //========================================================= 内部方法 ============================================================
    //==============================================================================================================================

    //==============================================================================================================================
    //========================================================= 外部方法 ============================================================
    //==============================================================================================================================

    /**
     * 向内存和本地存储数据
     * @param key 键值
     * @param data 数据
     * @param saveImmediately 是否立即存储到本地，默认是
    */
    setData(key: string, data: unknown, saveImmediately: boolean = true): void {
        // cc.log(`LocalStorage setData key = ${key} data = `, data)

        this._data = this._data || cc.js.createMap()
        this._data[key] = data
        saveImmediately && cc.sys.localStorage.setItem(key, JSON.stringify(data))
    }

    /**
     * 获取内存和本地数据
     * @param key 键值
     */
    getData<T>(key: string): T {
        // cc.log(`LocalStorage getDate key = ${key}`)

        this._data = this._data || cc.js.createMap()
        if (this._data[key] === undefined) {
            let data = cc.sys.localStorage.getItem(key)
            if (data !== null) {
                this._data[key] = JSON.parse(data)
            }
        }

        return this._data[key] == null ? null : this._data[key] as T;
    }

    /**
     * 删除内存和本地数据
     * @param key 键值
     */
    removeData(key: string) {
        // cc.log(`LocalStorage removeData key = ${key}`)

        delete this._data[key]
        cc.sys.localStorage.removeItem(key)
    }

    /**
     * 清空内存和本地数据
     */
    clearData() {
        // cc.log(`LocalStorage clearData`)

        this._data = cc.js.createMap()
        cc.sys.localStorage.clear()
    }
}